package movieGandalf.common;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Log {
        private final String LOG_FILE = "Gandalf_log.txt";
        private static Log inst = null;

        private Log() {
        }

        // This class is implemented using singleton design pattern
        public static Log getInstance() {
                if (inst == null) {
                        inst = new Log();
                }

                return inst;
        }

        // Wrapper function to write an exception stack
        public void WriteMessage(String message, Exception exp) {
                if (exp == null) {
                        this.WriteMessage(message);
                } else {

                        StringBuilder sb = new StringBuilder();

                        if (message != null && message.length() > 0) {
                                sb.append(message);
                                sb.append("\r\n\t");
                        }

                        sb.append(exp.getMessage());
                        if (exp.getStackTrace() != null) {
                                StackTraceElement[] stackTrace = exp.getStackTrace();
                                for (StackTraceElement curTrace : stackTrace) {
                                        sb.append("\r\n\t");
                                        sb.append(curTrace.toString());
                                }
                        }

                        this.WriteMessage(sb.toString());
                }
        }

        public void WriteMessage(Exception exp) {
                WriteMessage("", exp);
        }

        // Writess the given message to the log file
        public synchronized void WriteMessage(String message) {
                FileWriter fw = null;
                BufferedWriter bw = null;

                if (message != null) {
                        try {
                                fw = new FileWriter(LOG_FILE, true);
                                bw = new BufferedWriter(fw);

                                StringBuilder sb = new StringBuilder(message);
                                sb.insert(0, " - ");
                                sb.insert(0, (new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"))
                                                .format(Calendar.getInstance().getTime()));
                                sb.append('\r');
                                sb.append('\n');

                                bw.write(sb.toString());
                        } catch (IOException e) {
                                // Can't really do anything
                        } finally {
                                if (bw != null) {
                                        try {
                                                bw.close();
                                        } catch (IOException e1) {
                                        }
                                }

                                if (fw != null) {
                                        try {
                                                fw.close();
                                        } catch (IOException e1) {
                                        }
                                }
                        }
                }

        }
}